

Ext.define('SHApp.Sys.Function.Grid', {
    extend: 'Ext.tree.Panel',
    alias: 'widget.funcgrid',
    requires: ['Ext.ux.CheckColumn'],
    btnSaveID: '_save',
    btnCancelID: '_reset',
    //collapsible: true,
    pvalue: 0,
    pData: [],
    useArrows: true,
    rootVisible: false,
    multiSelect: true,
    singleExpand: false,
    columns: [{
        text: "Id",
        flex: 1,
        dataIndex: 'id',
        sortable: false,
        hidden: true
    }, {
        text: "名称",
        width: 200,
        dataIndex: 'Name',
        xtype: 'treecolumn',
        sortable: true
    }, {
        text: "模块ID",
        width: 100,
        dataIndex: 'ModuleID',
        sortable: true,
        hidden: true
    }, {
        text: "图标",
        width: 40,
        dataIndex: 'Image',
        sortable: true,
        renderer: function (v) {
            return "<img src='../../../css/images/" + v + ".png'>";
        }
    }, {
        //xtype: 'checkcolumn',
        text: '权限',
        dataIndex: 'Value',
        flex: 1,
        renderer: function (v, v1, record) {
            return this.showCheckBox(v, v1, record);
        }
    }
    ],
    initComponent: function () {
        var me = this;
        me.btnSaveID += me.id;
        me.btnCancelID += me.id;
        Ext.apply(this);
        this.loadMetaData();
        this.creatTbar();
        this.loadData();
        this.callParent(arguments);
        this.checkBtnPermission(me.pvalue);
    },
    addValue: function (id, value) {
        var store = this.getStore();
        var m = store.getById(id);
        m.set("ValueHad", value);
        m.setDirty();
    },

    showCheckBox: function (value, p, record) {

        var str1 = "<div>";
        for (var i = 0; i < this.pData.length; i++) {
            var item = this.pData[i];
            var d = item.FValue & value;

            var s = "";
            var haspValue = record.data.ValueHad & item.FValue;
            if (haspValue > 0) {
                s = 'checked="checked"';
            }

            if (d > 0) {
                str1 += Ext.String.format('<input name="{0}" onclick="javascript:sss(\'{0}\',\'{4}\')" value={3} type="checkbox" {2}>{1} &#160; </input>',
                    record.data.id,
                    item.FText,
                    s,
                    item.FValue,
                    this.id
                    );
            }
        }

        str1 += "</div>";
        return str1;
    },


    loadMetaData: function () {
        var me = this;
        Ext.Ajax.request({
            method: "GET",
            url: 'api/metadata?format=json',
            success: function (response) {
                var t = Ext.decode(response.responseText);
                me.pData = t.Data;
            }
        })

    },

    loadData: function () {
        var me = this;
        var store = new Ext.data.TreeStore({
            proxy: {
                type: 'ajax',
                url: 'api/rolepermission?format=json'
            },
            reader: {
                type: 'json',
                root: 'Data'//'00000000-0000-0000-0000-000000000000'
            },
            fields: ['leaf', 'text', 'Name', 'ModuleID', 'Image', 'Value', 'id', 'level', 'RoleID', 'ValueHad'],
            root: {
                text: '菜单',
                id: "00000000-0000-0000-0000-000000000000",
                expanded: true
            },
            sorters: [{
                property: 'leaf',
                direction: 'ASC'
            }]
        });
        me.store = store;
    },
    //针对单个按钮设置是否可见
    //value 总权限值
    //btnid 按钮的id
    //btnValue 按钮对应的权限
    checkPermission: function (value, btnid, btnValue) {
        var me = this;
        if (btnid == "") {
            return;
        }
        var btn = me.down('#' + btnid);
        if (btn != null) {
            var bl = (value & btnValue) == btnValue;
            btn.setVisible(bl);
        }
    },

    //验证按钮的权限
    checkBtnPermission: function (value) {
        var me = this;
        //save
        me.checkPermission(value, me.btnSaveID, 512);

        //reset
        me.checkPermission(value, me.btnCancelID, 1024);

    },
    loadByRoleID: function (roleID, roleName) {
        this.down("#" + this.btnSaveID).setDisabled(false);
        this.down("#" + this.btnCancelID).setDisabled(false);
        setCookie("RoleID", roleID);
        var store = this.store;
        store.proxy.url = "api/rolepermission?format=json&RoleID=" + roleID;
        store.load();
    },
    creatTbar: function () {
        var me = this;
        me.tbar = ['->',
                {
                    xtype: 'button',
                    disabled: true,
                    id: me.btnSaveID,
                    text: '保存',
                    iconCls: 'icon-btn-save',
                    handler: function () {
                        var RoleID = getCookie("RoleID");
                        me.store.proxy.url = "api/modelNode?format=json&RoleID=" + RoleID;
                        me.store.update();
                        Ext.Msg.alert("提示信息", "保存成功！");
                    }
                }, {
                    xtype: 'button',
                    disabled: true,
                    id: me.btnCancelID,
                    text: '重置',
                    iconCls: 'icon-btn-reset',
                    handler: function () {
                        me.store.load();
                    }
                }
            ]
    }
})

